#include <stdio.h>
#include <assert.h>
#include <memory.h>
#include <windows.h>
#include "ReadGPSmultiThread.h"

typedef unsigned __int32 uint32;
typedef unsigned __int64 uint64;

/*
Global state and function to generate random values and memory addresses
to drive the access patterns to memory
*/

bool run = false;
int	iterations = 2;
bool halt = false;

typedef struct experiment_state
{
	uint32 type;            // state for our experiment
	uint32 seed;
	uint32 stride;
	uint32 mask;

	uint32 iterations;      // state which used to be global, but is now shared for all threads
	uint32 length;
	uint32* thearray;
	uint32 arraycnt;

	uint64 tickFrequency;	// Ticks per second
	uint64 tickCountBegin;	// Initial tick position
	uint64 tickCountEnd;	// Final tick position

	uint32 sumcnt;          // state for the summary output
	uint64 sumsum;

	long int x1, x2;        // state for our PRNG
} t_expstate;

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
	t_expstate* threadstate = (t_expstate*)lpParameter;

	char SatelliteDerivedTime[20];
	int32_t satLen = 20;

	while ( iterations < 10 )
	{
		while (!run) {
			Sleep(200);
		}
		printf("T1 connects to func.\n");
		LesaSerialPort(iterations, SatelliteDerivedTime, &satLen);
		printf("T1: %s, len = %d\n", SatelliteDerivedTime, satLen);
		run = false;
	}
	halt = true;
	return 0;
}



DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
	t_expstate* threadstate = (t_expstate*)lpParameter;


	while (!halt)
	{
		if (!run) {
			printf("T2 set run to true\n");
			run = true;
			iterations++;
		}
		else {
			Sleep(200);
		}
	}
	/*
	char SatelliteDerivedTime[20];
	int32_t satLen = 20;
	LesaSerialPort(4, SatelliteDerivedTime, &satLen);
	printf("T2: %s, len = %d\n", SatelliteDerivedTime, satLen);
	*/

	return 0;
}

void main()
{
	HANDLE* threads = (HANDLE*) calloc(2, sizeof(HANDLE));

	t_expstate threadstate;
	threadstate.type = 0;

	threads[0] = CreateThread(NULL,				// default security attributes
							  0,			    // use default stack size
							  ThreadProc,		// thread function
							  &threadstate,		// argument to thread function
							  0,			    // use default creation flags
							  NULL);		    // returns the thread identifier

	// Check the return value for success.
	if (threads[0] == NULL)
	{
		printf("Couldn't start thread %d\n");
		exit(-1);
	}

	threads[1] = CreateThread(NULL,				// default security attributes
							  0,			    // use default stack size
							  ThreadProc2,		// thread function
							  &threadstate,		// argument to thread function
							  0,			    // use default creation flags
							  NULL);		    // returns the thread identifier

	// Check the return value for success.
	if (threads[1] == NULL)
	{
		printf("Couldn't start thread %d\n");
		exit(-1);
	}

	DWORD status = WaitForMultipleObjects(2, threads, TRUE, INFINITE);
	if (status == WAIT_FAILED)
	{
		printf("For some strange reason, couldn't wait for all the threads to finish.\n");
		exit(-1);
	}

	printf("hallo");

	CloseHandle(threads[0]);
	CloseHandle(threads[1]);

	system("pause");
}